package com.blog.controller;

import com.blog.dto.ArticleDTO;
import com.blog.dto.Result;
import com.blog.service.ArticleService;
import com.blog.utils.ArticleSearchUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@Slf4j
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleSearchUtils articleSearchUtils;
    @GetMapping("/public/article/get_article/{articleId}")
    public Result get_article(@PathVariable Integer articleId) {
        return articleService.getArticle(articleId);

    }
    @PostMapping("/article/addarticle")
    public Result addarticle(@RequestBody ArticleDTO article) {
        try {
            articleService.addArticle(article);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(500,"保存文章失败",null);
        }
    }

    @PostMapping("/article/pullimages")
    public Result uploadimages(@RequestParam("file") MultipartFile file) {

        return articleService.uploadimages(file);
    }

    @GetMapping("/public/article/pages")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        return articleService.getarticlecards(pageNum,pageSize);
    }

    @GetMapping("/public/sidebar")
    public Result get_sidebar() {
        return articleService.get_sidebar();
    }

    @GetMapping("/public/advanced")
    public Result advanced(@RequestParam String keyword) {
//        return Result.ok(articleSearchUtils.advancedSearch(keyword));
        return Result.ok();
    }
}
